package v7_test;

import cn.egame.common.cache.ICacheClient;
import cn.egame.common.cache.SCacheClient;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.ck.EGameCacheKey;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SeeRedis {
    private ICacheClient getCacheList() {
        return SCacheClient.getInstance("egame_list");
    }

    private ICacheClient getCache() {
        return SCacheClient.getInstance("egame");
    }

    public static void main(String[] args) {
        try {
            Utils.initLog4j();
            SeeRedis server = new SeeRedis();
            server.getDataFromRedis();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getDataFromRedis() throws Exception {
        try {
            String cacheKey = "";

            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("# 类型 1.输入缓存的key,查询缓存中的value");
            System.out.println("# 类型 2.输入游戏id ，删除关联的实体缓存(t_game,t_game_status)");
            System.out.println("# 类型 3. 输入缓存的key，删除该缓存");
            System.out.println("请选择类型数字：");
            String type = br.readLine();

            if ("1".equals(type)) {
                System.out.println("--请输入要查询缓存的key：");
                br = new BufferedReader(new InputStreamReader(System.in));
                cacheKey = br.readLine();
                getCacheValue(cacheKey);
            } else if ("2".equals(type)) {
                System.out.println("--请输入游戏id：");
                br = new BufferedReader(new InputStreamReader(System.in));
                String gId = br.readLine();
                deleteCacheByGId(gId);
            } else if ("3".equals(type)) {
                System.out.println("--请输入要删除缓存的key：");
                br = new BufferedReader(new InputStreamReader(System.in));
                cacheKey = br.readLine();
                deleteCacheByCacheKey(cacheKey);
            }
            System.exit(0);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
    }

    public void getCacheValue(String cacheKey) {
        Object value = new Object();
        value = getCache().get(cacheKey);
        if (value == null) {
            value = getCacheList().get(cacheKey);
        }
        System.out.println("/*======================================*/");
        System.out.println("    缓存的key为：       " + cacheKey);
        System.out.println("    缓存的value为：     " + value);
        System.out.println("/*======================================*/");
    }

    public void deleteCacheByGId(String inputGId) {
        int gId = Integer.parseInt(inputGId);

        getCache().remove(EGameCacheKey.getGameInfoById(gId));

        getCache().remove(EGameCacheKey.getGameStatusInternal(gId));

        System.out.println("/*======================================*/");
        System.out.println("    删除缓存的key为：       " + EGameCacheKey.getGameInfoById(gId));
        System.out.println("    删除缓存的key为：       " + EGameCacheKey.getGameStatusInternal(gId));
        System.out.println("/*======================================*/");
    }

    public void deleteCacheByCacheKey(String cacheKey) {
        getCache().remove(cacheKey);
        getCacheList().remove(cacheKey);
        System.out.println("/*======================================*/");
        System.out.println("    删除缓存的key为：       " + cacheKey);
        System.out.println("/*======================================*/");
    }
}